#include <stdio.h> 
#include <time.h> 
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdbool.h>
#include <string.h>

/* Manage Sensors Component*/
/**************************************************************************/
    /* Specify registry parameters for individual sensor */
	const unsigned int Window_Type = 0, Door_Type  = 1, Motion_Type = 3, Light_Type = 4;
	
	struct sensor_registry_entry {
							unsigned int sensor_number; //Sensor number (0-3) of sensor type
							unsigned int sensor_type;//Sensor type (window, door, motion, light
							unsigned int sensor_code;//Sensor specific ID code (not used in prototype)
							bool sensor_active; //Sensor is Active or Disabled
							struct sockaddr sensor_addr; //Socket ID info for sensor
							char	uid[20];	//User ID for sensor access	
                            char	passwd[20]; //Password for sensor access
                            unsigned int nonce; //Comm parameter
                            unsigned int cnonse; //Comm parameter
                            unsigned int hash; //Comm parameter
                            bool	fault_state; //Set TRUE if sensor does not respond normally to a poll
							};
    /* Declare sensor registry for 16 sensors */
	struct sensor_registry_entry sensor_registry[16];
	
	const bool Active = true, Disabled = false;
	const bool Open= true, Closed = false;
	const bool On = true, Off = false;
	const bool Motion = true, No_Motion = false;
	const bool Armed = true, Disarmed = false;
	const int Error = 1, No_Error = 0;
	

	
	/* Specify configuration parameters for an individual sensor */
	struct sensor_config  { bool	sensor_active; // sensor to ACTIVE or DISABLED state
                            bool    normal_state;  //Set Sensor normal state to OPEN(ON) or CLOSED(OFF)
							};
	/* Specify parameters for full system confuiguration (all sensors) */
	struct system_config  { bool system_state; 				//Set system state to ARMED or DISARMED
							struct sensor_config window[4]; // Config for all window sensors
							struct sensor_config door[4]; //Config for all door sensors
							struct sensor_config motion[4]; //Config for all motion sensors
							struct sensor_config light_control[4]; //Config for all light controls
					    	};
	struct system_config  default_sys_config; // Default system config established at startup
	struct system_config  current_sys_config; // Current system config established by the last command
	struct system_config  previous_sys_config; // Pending system config to be established by a new command
			
	/* System/sensor status structure*/
	
	const bool Fault = true, Normal = false;
	const bool Alarm = true, No_Alarm = false;
	
	struct sensor_status { bool  sensor_active; //Sensor is in ACTIVE or DISABLED state 
                       	   bool  sensor_alarm_state; //Sensor is in ALARM or NO_ALERM state
                       	   bool  sensor_fault; //FAULT if sensor polls fails, Normal if sensor poll is normal
						   };
						   
	struct system_status  { bool system_state; 				//Current system state is ARMED or DISARMED
							bool system_alarm_state;		//Current system alarm state us ALARM or NORMAL
							struct sensor_status window[4]; // Status for all window sensors
							struct sensor_status door[4]; //Status for all door sensors
							struct sensor_status motion[4]; //Status for all motion sensors
							struct sensor_status light_control[4]; //Status for all light controls
					    	};
	struct system_status	current_sys_status;
	
    typedef enum {Config, Status, Arm_System, Disarm_System} Command_type;
	bool	sensor_alarm_state, system_alarm_state;
	bool 	sensor_state;
	int		i, error;

/**************************************************************************/

/* Get next socket entry */
int next_socket_addr(struct sockaddr *socket_ptr)
   {
   
   };

/**************************************************************************
   Send Sensor Request Function
   
   This function sends a status request message to the sensor identified by 
   the registry_number parameter(index to the sensor_registry entry)*/

int send_sensor_request(int registry_number)
 	{
 	
 	};

/**************************************************************************
	Get Sensor Response Function
	
	This function returns the sensor_state (open/closed, motion/no-motion)that
	is returned by the sensor in response to send_sensor_request().  The 
	registry_number parameter indexes to the sensor_registry entry. */

bool sensor_state = get_sensor_response(int registry_number)
   {
   
   };

/**************************************************************************/

/* Initialize sensors and comm channels*/

int initialize_sensors()
   {
   
   };

/**************************************************************************/

/* Initialize sensor registry function */

int initialize_sensor_registry()
  {
     int registry_no, sensor_no;
     struct sockaddr *new_socket_ptr;
     
     registry_no = 0;
     
     /* Initialize window sensor unique registry entries */    
     for (sensor_no = 0; sensor_no <= 3; sensor_no++)
     {
        sensor_registry[registry_no].sensor_number = sensor_no;
        sensor_registry[registry_no].sensor_type = Window_Type;   
        registry_no++;
     };
     
      /* Initialize door sensor unique registry entries */    
     for (sensor_no = 0; sensor_no <= 3; sensor_no++)
     {
        sensor_registry[registry_no].sensor_number = sensor_no;
        sensor_registry[registry_no].sensor_type = Door_Type;   
        registry_no++;
     };
     
     /* Initialize motion sensor unique registry entries */    
     for (sensor_no = 0; sensor_no <= 3; sensor_no++)
     {
        sensor_registry[registry_no].sensor_number = sensor_no;
        sensor_registry[registry_no].sensor_type = Motion_Type;   
        registry_no++;
     };
     
     /* Initialize light control sensor unique registry entries */    
     for (sensor_no = 0; sensor_no <= 3; sensor_no++)
     {
        sensor_registry[registry_no].sensor_number = sensor_no;
        sensor_registry[registry_no].sensor_type = Light_Type;   
        registry_no++;
     };
     
     /* Initilize fields common to all registry entries */
     for (registry_no = 0; registry_no <= 15; registry_no++)
     {
       sensor_registry[registry_no].sensor_code = 0; //Unused in protptype
       sensor_registry[registry_no].sensor_active = Disabled;  //initially disabled
       sensor_registry[registry_no].fault_state = Normal;
       error = next_socket_addr(new_socket_ptr);
       if (error != 0) printf ("Registry socket initialization error");
       sensor_registry[registry_no].sensor_addr = *new_socket_ptr;
       /* Initialize registry comm parameters */
       
       /* Initialize sensor userID and password to default values since they are not used in the prototype*/
       
       strcpy(&(sensor_registry[registry_no].uid[0]), "anysensor"); //set default UID (
       strcpy(&(sensor_registry[registry_no].passwd[0]), "nopass"); //set default password
     };
     return(No_Error);
  };

/**************************************************************************
	Activate New Alarm Function (Not implemented in prototype)
	
	This function will activate the on-premises alarm and optionally contact
	designated authorities  */
bool activate_new_alarm()
   {
   
   };

/**************************************************************************/

/*Check System Status Function
	Poll each enabled sensor and check for change in state.
	Flag an alarm if any sensor is not in normal state*/

int check_system_status()
  {
  int registry_no;
  bool alarm_state;
  
  /* Check status of each sensor in the registry. */
  
  for (registry_no = 0; registry_no <= 15; registry_no++)
  	{
  	  alarm_state = check_sensor_state(registry_no);
    	if (alarm_state == Alarm) 
    	{
    	 /* If there was no previous alarm, active alarm mode (call police, start siren) */
    	  if (current_sys_status.system_alarm_state != Alarm) activate_new_alarm(); 
  	      current_sys_status.system_alarm_state = Alarm; //Set system alarm state
  	    };
  	};
  	return(current_sys_status.system_alarm_state); //Return with current system alarm state    
  };

/**************************************************************************/

/* Check Sensor State Function 
   Poll the specified sensor number in the registry. If the sensor is not in normal state, 
   flag an alarm in the current system status. Otherwise, return that the state is normal.
   
   If a fault occurs, set fault flag in the sensor's registry entry*/
   
int check_sensor_state (int registry_number)
   {
      int	sensor_no;
    
      /* If the specified sensor is Disabled, always return with normal status */
  
      if (sensor_registry[registry_number].sensor_active == Disabled) return(0);
  
      /*  If the specified sensor is a light control, always return with normal status */
  
      if (sensor_registry[registry_number].sensor_type == Light_Type) return(0);
  
      /* If the specified sensor is already in fault state, return with an alarm status.  
      NOTE: Fault state is reset only when the system config is updated from the command interface*/
  
      if (sensor_registry[registry_number].fault_state == Fault) return(Alarm);
  
      /* Send status request message to sensor and wait until response is received. */
  
      send_sensor_request(registry_number);
      sensor_state = get_sensor_response();
   
      /* If the sensor returned a fault, set the fault flag in the registry entry. 
        Since the sensor may have been tampered with, return with Alarm status */
   
      if (sensor_state == Fault)
  	     {
  	       sensor_registry[registry_number].fault_state = Fault;
  	       return(Alarm);
  	     };
  
      /*Check if returned state is Normal per the current system configuration for the sensor type */
   
      sensor_no = sensor_registry[registry_number].sensor_number; //Get the sensor number from the registry
   
      /* Check for window sensor alarm */
      if (sensor_registry[registry_number].sensor_type == Window_Type)
         { 
           if (current_sys_config.window[sensor_no].normal_state != sensor_state)
               {
            	  current_sys_status.window[sensor_no].sensor_alarm_state = Alarm;
            	  return(Alarm);
                }
           else current_sys_status.window[sensor_no].sensor_alarm_state = No_Alarm;
         };

      /* Check for door sensor alarm */
      if (sensor_registry[registry_number].sensor_type == Door_Type)
         {
           if (current_sys_config.door[sensor_no].normal_state != sensor_state)
               {
            	  current_sys_status.door[sensor_no].sensor_alarm_state = Alarm;
            	  return(Alarm);
               }
           else current_sys_status.door[sensor_no].sensor_alarm_state = No_Alarm;
         };

      /* Check for motion sensor alarm */
      if (sensor_registry[registry_number].sensor_type == Motion_Type)
         {
           if (current_sys_config.motion[sensor_no].normal_state != sensor_state)
               {
            	  current_sys_status.motion[sensor_no].sensor_alarm_state = Alarm;
             	  return(Alarm);
               }        	
           else current_sys_status.motion[sensor_no].sensor_alarm_state = No_Alarm;
         };
      return(No_Alarm);
    };

/**************************************************************************
 Command Pending Function
 Check if there is a pending command from the Command Interface (non-blocking)*/
int command_pending()
   {
   
   };
/*************************************************************************
  Get Command Function
  Get the next command string from Command Interface */
  
Command_type get_command()
   {
   
   };
/**************************************************************************
   Get New Configuration
   Get the configuration string sent with the last Configure command*/
   
int get_new_config(char *new_config_str)
   {
   
   };

/**************************************************************************/
/* Send Command Response
   Send response to a command from Command Interface */

int send_command_response(char *response_str)
   {
   
   };

/**************************************************************************/
/* Process Config Command
	Get the configuration parameters string from Command Interface 
	and build a new configuration data structure */
	
int process_config_command(struct system_config *new_config)
   {
      int error, registry_no, sensor_group, sensor_no;
      char response_str[20], new_config_str[17], *str_ptr, sensor_id_tag;
      
      previous_sys_config = current_sys_config;  //Save previous system config in case of error
      
      error = get_new_config(new_config_str); //Get the configuraton string
      if (error != 0) return(Error);  //Return error if there is no config string
      
      /* Read command string one character at a time to validate format and build the new system config structure
      in order of sensor groups(Door, Window, Motion, Light COntrol)*/
      str_ptr = new_config_str; //start at beginning of string
      sensor_id_tag = 'A'; //Initialize sensor tag for first sensor
      registry_no = 0; //Start with first registry entry
      
      /* Process config for window sensors */
      for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
      {
         if (*str_ptr != 'S') return(Error); //Check for sensor prefix character
         str_ptr++;//move to next character
         
         if (*str_ptr != sensor_id_tag) return(Error);//Check for sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Check if sensor active of disabled */
         if (*str_ptr == 'A') new_config->window[sensor_no].sensor_active = Active;
         if (*str_ptr == 'D') new_config->window[sensor_no].sensor_active = Disabled;
         else return(Error);
         str_ptr++;//move to next character
            
         /* Check if normal window sensor state is open or closed*/
         if (*str_ptr == 'O') new_config->window[sensor_no].normal_state = Open;
         if (*str_ptr == 'C') new_config->window[sensor_no].normal_state = Closed;
         else return(Error); 
         str_ptr++;//move to next character

         /* Set sensor active or disabled status in registry entry and reset faults*/
         sensor_registry[registry_no].fault_state = false; //Reset fault flag
         sensor_registry[registry_no].sensor_active = new_config->window[sensor_no].sensor_active;
         registry_no++; 	
      };
         
      /* Process config for door sensors */
      for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
      {
         if (*str_ptr != 'S') return(Error); //Check for sensor prefix character
         str_ptr++;//move to next character
         
         if (*str_ptr != sensor_id_tag) return(Error);//Check for sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Check if sensor active of disabled */
         if (*str_ptr == 'A') new_config->door[sensor_no].sensor_active = Active;
         if (*str_ptr == 'D') new_config->door[sensor_no].sensor_active = Disabled;
         else return(Error);
         str_ptr++;//move to next character
            
         /* Check if normal window sensor state is open or closed*/
         if (*str_ptr == 'O') new_config->door[sensor_no].normal_state = Open;
         if (*str_ptr == 'C') new_config->door[sensor_no].normal_state = Closed;
         else return(Error); 
         str_ptr++;//move to next character

         /* Set sensor active or disabled status in registry entry and reset faults*/
         sensor_registry[registry_no].fault_state = false; //Reset fault flag
         sensor_registry[registry_no].sensor_active = new_config->door[sensor_no].sensor_active;
         registry_no++; 	
      };

      /* Process config for motion sensors */
      for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
      {
         if (*str_ptr != 'S') return(Error); //Check for sensor prefix character
         str_ptr++;//move to next character
         
         if (*str_ptr != sensor_id_tag) return(Error);//Check for sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Check if sensor active of disabled */
         if (*str_ptr == 'A') new_config->motion[sensor_no].sensor_active = Active;
         if (*str_ptr == 'D') new_config->motion[sensor_no].sensor_active = Disabled;
         else return(Error);
         str_ptr++;//move to next character
            
         /* Check if normal window sensor state is open or closed*/
         if (*str_ptr == 'O') new_config->motion[sensor_no].normal_state = Motion;
         if (*str_ptr == 'C') new_config->motion[sensor_no].normal_state = No_Motion;
         else return(Error); 
         str_ptr++;//move to next character

         /* Set sensor active or disabled status in registry entry and reset faults*/
         sensor_registry[registry_no].fault_state = false; //Reset fault flag
         sensor_registry[registry_no].sensor_active = new_config->motion[sensor_no].sensor_active;
         registry_no++; 	
      };

      /* Process config for light control sensors */
      for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
      {
         if (*str_ptr != 'S') return(Error); //Check for sensor prefix character
         str_ptr++;//move to next character
         
         if (*str_ptr != sensor_id_tag) return(Error);//Check for sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Check if sensor active of disabled */
         if (*str_ptr == 'A') new_config->light_control[sensor_no].sensor_active = Active;
         if (*str_ptr == 'D') new_config->light_control[sensor_no].sensor_active = Disabled;
         else return(Error);
         str_ptr++;//move to next character
            
         /* Check if normal window sensor state is open or closed*/
         if (*str_ptr == 'O') new_config->light_control[sensor_no].normal_state = On;
         if (*str_ptr == 'C') new_config->light_control[sensor_no].normal_state = Off;
         else return(Error); 
         str_ptr++;//move to next character

         /* Set sensor active or disabled status in registry entry and reset faults*/
         sensor_registry[registry_no].fault_state = false; //Reset fault flag
         sensor_registry[registry_no].sensor_active = new_config->light_control[sensor_no].sensor_active;
         registry_no++; 	
      };
      *str_ptr = 0; //Set null to terminate response string
      strcpy(response_str, "CONFIG_SUCCESS"); 
      error = send_command_response(response_str);
      if (error != 0) printf("Error sending status command response");
      
      /* "CONFIG_FAILED" status not supported in prototype */
      
      return(0); //no error return
   };

/**************************************************************************/

/**************************************************************************/
/* Process Status Command
	Build a status message from the specified config structure and send to Command Interface */
	
int process_status_command(struct system_status *current_status)
   {
     char status_str[25]; //character status response buffer
     char response_str[25]; //command response string buffer
     int  error, registry_no, sensor_group, sensor_no;
     char *str_ptr, sensor_id_tag;
     
     /* Send Status Command Response */
     strcpy(response_str, "STATUS_REPLY");    
     send_command_response(response_str);
      
	 /* Send System Status Response 
	 	Alarm, Armed Normal, or Unarmed*/
	 
	 if (current_sys_status.system_alarm_state == Alarm) //Send Alarm message
	    {
	      strcpy(response_str, "SYSTEM_ALARM");    
          send_command_response(response_str);
	    };
	 if (current_sys_status.system_state == Armed)    //send Armed Normal message
	    {
	      strcpy(response_str, "SYSTEM_ARMED_NORMAL");    
          send_command_response(response_str);
	    }
	 else //Send Unarmed message
	    {
	      strcpy(response_str, "SYSTEM_UNARMED");    
          send_command_response(response_str);
	    };
     
     /* Build command string one character at a time from current_status structure
     in order of sensor groups(Door, Window, Motion, Light COntrol)*/
      
     str_ptr = status_str; //start at beginning of string
     sensor_id_tag = 'A'; //Initialize sensor tag for first sensor
     registry_no = 0; //Start with first registry entry
      
     /* Process status for window sensors */
     for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
     {
         *str_ptr = 'S'; //Add for sensor prefix character 
         str_ptr++;//move to next character
         
         *str_ptr = sensor_id_tag; //Add sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Status of  window sensor is active or disabled */
         if (current_status->window[sensor_no].sensor_active == Active)  *str_ptr = 'A';
         if (current_status->window[sensor_no].sensor_active == Disabled)  *str_ptr = 'D';
         str_ptr++;//move to next character
            
         /* Status of sensor state is Normal, Alarm, or Fault state*/
         if (current_status->window[sensor_no].sensor_alarm_state == Alarm)  *str_ptr = 'A';
         if (current_status->window[sensor_no].sensor_alarm_state == Normal)  *str_ptr = 'N';
         if (current_status->window[sensor_no].sensor_fault == Fault)  *str_ptr = 'F';     
         str_ptr++;//move to next character
     };

     /* Process status for door sensors */
     for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
     {
         *str_ptr = 'S'; //Add for sensor prefix character 
         str_ptr++;//move to next character
         
         *str_ptr = sensor_id_tag; //Add sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Status of  window sensor is active or disabled */
         if (current_status->door[sensor_no].sensor_active == Active)  *str_ptr = 'A';
         if (current_status->door[sensor_no].sensor_active == Disabled)  *str_ptr = 'D';
         str_ptr++;//move to next character
            
         /* Status of sensor state is Normal, Alarm, or Fault state*/
         if (current_status->door[sensor_no].sensor_alarm_state == Alarm)  *str_ptr = 'A';
         if (current_status->door[sensor_no].sensor_alarm_state == Normal)  *str_ptr = 'N';
         if (current_status->door[sensor_no].sensor_fault == Fault)  *str_ptr = 'F';     
         str_ptr++;//move to next character
     };

     /* Process status for motion sensors */
     for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
     {
         *str_ptr = 'S'; //Add for sensor prefix character 
         str_ptr++;//move to next character
         
         *str_ptr = sensor_id_tag; //Add sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Status of  window sensor is active or disabled */
         if (current_status->motion[sensor_no].sensor_active == Active)  *str_ptr = 'A';
         if (current_status->motion[sensor_no].sensor_active == Disabled)  *str_ptr = 'D';
         str_ptr++;//move to next character
            
         /* Status of sensor state is Normal, Alarm, or Fault state*/
         if (current_status->motion[sensor_no].sensor_alarm_state == Alarm)  *str_ptr = 'A';
         if (current_status->motion[sensor_no].sensor_alarm_state == Normal)  *str_ptr = 'N';
         if (current_status->motion[sensor_no].sensor_fault == Fault)  *str_ptr = 'F';     
         str_ptr++;//move to next character
     };

     /* Process status for light control sensors */
     for (sensor_no = 0; sensor_no <= 3; sensor_no++) //Process each window sensor number
     {
         *str_ptr = 'S'; //Add for sensor prefix character 
         str_ptr++;//move to next character
         
         *str_ptr = sensor_id_tag; //Add sensor ID char in correct sequence
         str_ptr++;//move to next character
         sensor_id_tag++; //update sensor ID char ('A' to 'F')
         
         /* Status of  window sensor is active or disabled */
         if (current_status->light_control[sensor_no].sensor_active == Active)  *str_ptr = 'A';
         if (current_status->light_control[sensor_no].sensor_active == Disabled)  *str_ptr = 'D';
         str_ptr++;//move to next character
            
         /* Light control sensor alarm state is Normal or Fault state*/
         if (current_status->light_control[sensor_no].sensor_fault == Fault)  *str_ptr = 'F';
         else *str_ptr = 'N';     
         str_ptr++;//move to next character
     };
     *str_ptr = 0; //Append null character
     send_command_response(status_str);// Send sensor status string
     return(0);//Normal return
   };

/**************************************************************************/

/**************************************************************************/
/* Process Arm Command */

int process_arm_command()
   {
      char response_str[20];
      /* Set system state to Armed */
      current_sys_status.system_state = Armed;

      /* Send command response */
      strcpy(response_str, "SYSTEM_ARMED"); 
      /* ARM_ERROR not supported in prototype */   
      send_command_response(response_str);

      return(0);
   };

/**************************************************************************/

/**************************************************************************/
/* Process Disarm Command */

int process_disarm_command()
   {
      char response_str[20];
      /* Set system state to Unarmed */
      current_sys_status.system_state = Disarmed;

      /* Send command response */
      strcpy(response_str, "SYSTEM_DISARMED"); 
      /* DISARM_ERROR not supported in prototype */   
      send_command_response(response_str);

      return(0);
   };

/**************************************************************************/
/* Process Command Error Function
   Send response for an invalid command tht does not meet interface spec */

int process_command_error()
   {
	  char response_str[25];
	  
	  strcpy(response_str, "COMMAND_ERROR");    
      send_command_response(response_str);
   
   };

/**************************************************************************/
/* Main Manage Sensors function*/

int main (int argc, char **argv)
{

bool	command_received;

Command_type command;

int  error;
	
/* Initialize sensor registry 
    Initialize the registry entry for each sensor */
	error = initialize_sensor_registry();
	if (error != 0) 
		{
			printf("Registry initialization failed\n");
			exit(1);
		};
/* Initiate communication with sensors and poll initial status */
	error = initialize_sensors();
	if (error != 0) 
		{
			printf("Sensor initialization failed\n");
			exit(1);
		};	
/* COnfigure the system to the default configuration:
	System DISARMED and all sensors DISABLED */

/* Initialize default system configuration*/
	
	default_sys_config.system_state = Disarmed;
	for (i=0; i<=3; i++)
		{
			default_sys_config.window[i].sensor_active = Disabled;
			default_sys_config.window[i].normal_state = Closed;
			default_sys_config.door[i].sensor_active = Disabled;
			default_sys_config.door[i].normal_state = Closed;
			default_sys_config.motion[i].sensor_active = Disabled;
			default_sys_config.motion[i].normal_state = No_Motion;
			default_sys_config.light_control[i].sensor_active = Disabled;
			default_sys_config.light_control[i].normal_state = Off;
		};
	
	/* set current system configuration to default system configuration */

	current_sys_config = default_sys_config;

/* Main Sensor Control Function:

	This function executes the main sensor control loop theat executes once every 100 milliseconds (approximately) 
	In each loop polls the current status of all sensors and processes one command messages 
	received from the Command Interface module. */

main_loop:

	/* Wait for 100 ms */
	usleep(100000);

	/*Check status of sensors and process any alarms */
	
	system_alarm_state = check_system_status();
	if (system_alarm_state = Alarm) printf("*****System Alarm State*****\n");

	/* If no sensor command has been received from Command Interface, return to the top of the main loop */
	
	command_received = command_pending();
	if (command_received == false) goto main_loop; 

	/*	Get one command.  Process the command if valid or return an error.
	Note: If more than one command command is pending, it will be processed in the next loop*/
	
	command = get_command();
	
	if (command == Config) error = process_config_command(&current_sys_config);
	if (command == Status) error = process_status_command(&current_sys_status);
	if (command == Arm_System) error = process_arm_command();
	if (command == Disarm_System) error = process_disarm_command();
	else process_command_error(); //invalid command
	
	if (error != 0) process_command_error(); //Command processing error occurred

	goto main_loop; //Start next polling cycle
}	